﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Dataweb.NShape.Advanced;
using Dataweb.NShape;
using cz.zcu.PreSti.Components;
using System.Drawing;
using System.ComponentModel;

namespace cz.zcu.PreSti.Shapes
{
    class SoundStimulusSymbol : StimulusSymbol
    {
        protected internal SoundStimulusSymbol(ShapeType shapeType, Template template)
            : base(shapeType, template)
        {
        }

        public override BaseComponent initializeComponent(IPresentation p)
        {
            SoundStimulusModel model = (SoundStimulusModel)this.ModelObject;
            SoundStimulus stimulus = new SoundStimulus(p);
            stimulus.Delay = model.Duration;

            stimulus.SoundFilePath = model.SoundFilePath.ElementAt(0);
            return stimulus;
        }

        protected override void InitializeToDefault(IStyleSet styleSet)
        {
            base.InitializeToDefault(styleSet);
            Text = "Sound";
        }

        internal static Shape CreateInstance(ShapeType shapeType, Template template)
        {
            return new SoundStimulusSymbol(shapeType, template);
        }

        public override Shape Clone()
        {
            Shape result = new SoundStimulusSymbol(Type, (Template)null);
            result.CopyFrom(this);
            return result;
        }
    }

    public class SoundStimulusModel : ModelObjectBase
    {
        #region Fields

        private List<String> soundFilePath = new List<string>()
        {
            SoundStimulus.CHOOSER_MSG,
            SoundStimulus.SoundNames.beep.ToString(),
            SoundStimulus.SoundNames.boom.ToString(),
            SoundStimulus.SoundNames.ring.ToString(),
            SoundStimulus.SoundNames.scratching.ToString()
        };
        private int duration = 5000;
        private char securityDomainName = 'A';

        private const int PropertyIdImagePath = 1;
        private const int PropertyIdDuration = 2;

        #endregion

        #region properties

        [Category("Presentation")]
        [Description("Duration of the stimulus in miliseconds.")]
        [PropertyMappingId(PropertyIdDuration)]
        public virtual int Duration
        {
            get
            {
                return duration;
            }
            set
            {
                duration = value;
            }
        }

        [Category("Presentation")]
        [Description("Sound effect - use \"...\" button and arrows to move chosen sound to the top.")]
        [Editor("System.Windows.Forms.Design.StringCollectionEditor",
        typeof(System.Drawing.Design.UITypeEditor))]
        [PropertyMappingId(PropertyIdImagePath)]
        public virtual List<string> SoundFilePath
        {
            get
            {
                return soundFilePath;
            }
            set
            {
                soundFilePath = value;
            }
        }

        #endregion

        public static SoundStimulusModel CreateInstance(ModelObjectType modelObjectType)
        {
            return new SoundStimulusModel(modelObjectType);
        }

        protected internal SoundStimulusModel(ModelObjectType modelObjectType)
            : base(modelObjectType)
        {
            this.terminalCount = 3;
        }

        protected internal SoundStimulusModel(SoundStimulusModel source)
            : base(source)
        {
            Duration = source.Duration;
            SoundFilePath = source.SoundFilePath;
            this.terminalCount = 3;
        }


        public override IModelObject Clone()
        {
            return new SoundStimulusModel(this);
        }


        public override IEnumerable<MenuItemDef> GetMenuItemDefs()
        {
            throw new NotImplementedException();
        }


        public override void Connect(TerminalId ownTerminalId, IModelObject targetConnector, TerminalId targetTerminalId)
        {
        }


        public override void Disconnect(TerminalId ownTerminalId, IModelObject targetConnector, TerminalId targetTerminalId)
        {
        }

        public override char SecurityDomainName
        {
            get { return securityDomainName; }
            set
            {
                if (value < 'A' || value > 'Z')
                    throw new ArgumentOutOfRangeException("SecurityDomainName", "The domain qualifier has to be an upper case  ANSI letter (A-Z).");
                securityDomainName = value;
            }
        }


        #region IEntity Members

        public static new IEnumerable<EntityPropertyDefinition> GetPropertyDefinitions(int version)
        {
            foreach (EntityPropertyDefinition pi in ModelObjectBase.GetPropertyDefinitions(version))
                yield return pi;
            yield return new EntityFieldDefinition("Duration", typeof(int));
            yield return new EntityFieldDefinition("SoundFilePath", typeof(List<string>));
        }

        protected override void LoadFieldsCore(IRepositoryReader reader, int version)
        {
            base.LoadFieldsCore(reader, version);
            Duration = reader.ReadInt32();
            SoundFilePath = new List<string>(){reader.ReadString()};
        }

        protected override void SaveFieldsCore(IRepositoryWriter writer, int version)
        {
            base.SaveFieldsCore(writer, version);
            writer.WriteInt32(Duration);
            writer.WriteString(SoundFilePath.ElementAt(0));
        }

        #endregion


    }
}
